package com.yun.datastructure.map.hashmap.leetcode;

import java.util.Arrays;

public class Leetcode242 {
    public boolean isAnagram1(String s, String t) {
        int[] map=new int[26];
        int sLen=s.length();
        int tLen=t.length();
        if(sLen!=tLen){
            return false;
        }
        for (int i = 0; i < sLen; i++) {
            int idx=s.charAt(i)-97;
            map[idx]++;
        }
        for (int i = 0; i < tLen; i++) {
            int idx=t.charAt(i)-97;
            map[idx]--;
            if(map[idx]<0){
                return false;
            }
        }
        return true;
    }

    public boolean isAnagram(String s, String t){
        return Arrays.equals(getKey(s),getKey(t));
    }

    private static int[] getKey(String s){
        int[] array=new int[26];
        char[] chars = s.toCharArray();
        for (char ch : chars) {
            array[ch-97]++;
        }
        return array;
    }
}
